package com.gkfreshmart.cloud.api;

/**
 * Copyright (C),2019-2020),田坊集市数据科技
 * FileName: ResultUtil
 * Author: Bruin
 * Date: 2020/10/16 15：02
 * Description: 响应结果
 * History:
 * <author>  <time>  <version>   <desc>
 * 作者姓名  修改时间    版本号     描述
 */
public class ResultWrapper {
    private ResultWrapper(){

    }
    private static final String DEFAULT_SUCCESS_MESSAGE = "SUCCESS";
    private static final String DEFAULT_FAIL_MESSAGE = "FAIL";
    private static final String DEFAULT_NOT_DATA_MESSAGE = "NOT FOUND DATA";

    /**
     * 默认响应成功
     *
     * @return
     */

    public static Result ok() {
        return new Result()
                .setCode(ResultCodeEnum.SUCCESS)
                .setMessage(DEFAULT_SUCCESS_MESSAGE);
    }

    /**
     * 带消息响应成功
     *
     * @param msg
     * @return
     */
    public static Result ok(String msg) {
        return new Result()
                .setCode(ResultCodeEnum.SUCCESS)
                .setMessage(msg);
    }

    /**
     * 带数据响应成功
     *
     * @param data
     * @return
     */
    public static Result ok(Object data) {
        return new Result()
                .setCode(ResultCodeEnum.SUCCESS)
                .setMessage(DEFAULT_SUCCESS_MESSAGE)
                .setData(data);
    }
    public static Result isOk(boolean flag) {
        if (flag) {
            return new Result()
                    .setCode(ResultCodeEnum.SUCCESS)
                    .setMessage(DEFAULT_SUCCESS_MESSAGE)
                    .setData(null);

        } else {
            return new Result()
                    .setCode(ResultCodeEnum.FAIL)
                    .setMessage(DEFAULT_NOT_DATA_MESSAGE)
                    .setData(null)
                    ;
        }

    }
    public static Result isQueryOk(Object data) {
        if (data == null) {
            return new Result()
                    .setCode(ResultCodeEnum.FAIL)
                    .setMessage(DEFAULT_NOT_DATA_MESSAGE)
                    ;
        } else {
            return new Result()
                    .setCode(ResultCodeEnum.SUCCESS)
                    .setMessage(DEFAULT_SUCCESS_MESSAGE)
                    .setData(data);
        }

    }
    public static Result isActionOk(Integer flag) {
        if (flag < 0) {
            return new Result()
                    .setCode(ResultCodeEnum.FAIL)
                    .setMessage(DEFAULT_FAIL_MESSAGE)
                    .setData(flag);
        } else {
            return new Result()
                    .setCode(ResultCodeEnum.SUCCESS)
                    .setMessage(DEFAULT_SUCCESS_MESSAGE)
                    .setData(flag);
        }

    }


    public static Result ok(ResultCodeEnum code) {
        return new Result()
                .setCode(code)
                .setMessage(code.getMsg());
    }

    public static Result ok(ResultCodeEnum code, Object data) {
        return new Result()
                .setCode(code)
                .setMessage(code.getMsg())
                .setData(data);
    }

    /**
     * 带消息和数据响应成功
     *
     * @param data
     * @return
     */
    public static Result ok(String msg, Object data) {
        return new Result()
                .setCode(ResultCodeEnum.SUCCESS)
                .setMessage(msg)
                .setData(data);
    }

    /**
     * 默认失败
     *
     * @return
     */
    public static Result fail() {
        return new Result()
                .setCode(ResultCodeEnum.FAIL)
                .setMessage(DEFAULT_FAIL_MESSAGE);
    }

    /**
     * 带消息失败
     *
     * @return
     */
    public static Result fail(String msg) {
        return new Result()
                .setCode(ResultCodeEnum.FAIL)
                .setMessage(msg);
    }

    /**
     * 带数据失败
     *
     * @return
     */
    public static Result fail(Object data) {
        return new Result()
                .setCode(ResultCodeEnum.FAIL)
                .setMessage(DEFAULT_FAIL_MESSAGE)
                .setData(data);
    }

    /**
     * 带数据失败
     *
     * @return
     */
    public static Result fail(String msg, Object data) {
        return new Result()
                .setCode(ResultCodeEnum.FAIL)
                .setMessage(msg)
                .setData(data);
    }
    public static Result fail(ResultCodeEnum resultCodeEnum, String message) {
        return new Result()
                .setCode(resultCodeEnum)
                .setMessage(message);
    }
}


